﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

public class OrderService
{
    private List<Order> orders = new List<Order>();

    // 添加订单
    public void AddOrder(Order order)
    {
        if (orders.Any(o => o.OrderId == order.OrderId))
            throw new ArgumentException($"订单 {order.OrderId} 已存在");
        orders.Add(order);
    }

    // 删除订单
    public void RemoveOrder(string orderId)
    {
        var order = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order == null) throw new ArgumentException($"订单 {orderId} 不存在");
        orders.Remove(order);
    }

    // 修改订单
    public void UpdateOrder(Order order)
    {
        var index = orders.FindIndex(o => o.OrderId == order.OrderId);
        if (index < 0) throw new ArgumentException($"订单 {order.OrderId} 不存在");
        orders[index] = order;
    }

    // 查询方法
    public List<Order> GetAllOrders() => orders.OrderBy(o => o.OrderId).ToList();

    public List<Order> QueryByOrderId(string orderId) =>
        orders.Where(o => o.OrderId.Contains(orderId))
              .OrderBy(o => o.TotalAmount)
              .ToList();

    public List<Order> QueryByProductName(string productName) =>
        orders.Where(o => o.Details.Any(d => d.Product.Name.Contains(productName)))
              .OrderBy(o => o.TotalAmount)
              .Distinct()
              .ToList();

    public List<Order> QueryByCustomer(Customer customer) =>
        orders.Where(o => o.Customer.Equals(customer))
              .OrderBy(o => o.TotalAmount)
              .ToList();

    public List<Order> QueryByAmountRange(decimal min, decimal max) =>
        orders.Where(o => o.TotalAmount >= min && o.TotalAmount <= max)
              .OrderBy(o => o.TotalAmount)
              .ToList();

    // 排序方法
    public void SortOrders() => orders.Sort();
    public void SortByTotalAmount() => orders.Sort((a, b) => a.TotalAmount.CompareTo(b.TotalAmount));
    public void SortOrders(Comparison<Order> comparison) => orders.Sort(comparison);

    // XML序列化
    public void Export(string filePath)
    {
        if (string.IsNullOrWhiteSpace(filePath))
            throw new ArgumentException("文件路径无效");

        using (var writer = new StreamWriter(filePath))
        {
            new XmlSerializer(typeof(List<Order>)).Serialize(writer, orders);
        }
    }

    // XML反序列化
    public void Import(string filePath)
    {
        if (!File.Exists(filePath))
            throw new FileNotFoundException("文件不存在", filePath);

        List<Order> importedOrders;
        using (var reader = new StreamReader(filePath))
        {
            importedOrders = (List<Order>)new XmlSerializer(typeof(List<Order>))
                .Deserialize(reader);
        }

        foreach (var order in importedOrders)
        {
            if (orders.Any(o => o.OrderId == order.OrderId))
                throw new InvalidOperationException($"订单 {order.OrderId} 已存在");
        }

        orders.AddRange(importedOrders);
    }
}